const supplierModel = require('./model/supplierModel')


exports.loginDao = async function(info) {
    const res = await supplierModel.findOne({
        c_telphone: info.c_email,
        c_password: info.c_password
    })
    return res
}

exports.registerDao = async function(info) {
    return await supplierModel.create(info)
}

exports.getSupplierByName = async function(info) {
    return await supplierModel.findOne({
        c_name: info.c_name,
    })
}

exports.getSupplierByCode = async function(info) {
    return await supplierModel.findOne({
        c_code: info.c_code,
    })
}

exports.getSupplierByEmail = async function(info) {
    return await supplierModel.findOne({
        c_email: info.c_email,
    })
}

exports.getSupplierByTelphone = async function(info) {
    return await supplierModel.findOne({
        c_telphone: info.c_telphone,
    })
}

exports.updateSupplier = async function(info) {
    return await supplierModel.findOneAndUpdate({_id: info._id}, info)
}

exports.getSupplierListDao = async function(info) {
    const skip = (info.page_num - 1) * info.page_size
    const query = supplierModel.find()
    if (info.c_state) {
        query.where('c_state').equals(info.c_state)
    } else {
        query.where('c_state').in(['0', '1', '2', '100'])
    }
    return await query.skip(skip).limit(info.page_size).exec()
}
exports.getSupplierCountDao = async function(info) {
    const query = supplierModel.find()
    if (info.c_state) {
        query.where('c_state').equals(info.c_state)
    } else {
        query.where('c_state').in(['0', '1', '2', '100'])
    }
    return await query.countDocuments()
}

exports.updateSupplierStateDao = async function(info) {
    return await supplierModel.updateOne({_id: info.c_id}, { c_state: info.action})
}
